/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import Data.GheNgoi;
import Data.GiuongNam;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author bienphongvnn
 */
public class GiuongNamModel {
    public static ArrayList<GiuongNam> ListGiuongNam() {
        ArrayList<GiuongNam> ds = new ArrayList<GiuongNam>();
        Connection con = ConnectionProvider.Connect();
        try {
            String sql = "SELECT * FROM giuongnam";
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                GiuongNam giuong = new GiuongNam();
                giuong.SoHieuGiuong = rs.getInt("SoHieuGiuong");
                giuong.TrangThai = rs.getString("TrangThai");
                giuong.Tang = rs.getInt("Tang");
                giuong.MaPhong = rs.getString("MaPhong");
                giuong.MaKhachHang = rs.getString("MaKhachHang");
                giuong.MaToa = rs.getString("MaToa");
                giuong.MaTau = rs.getString("MaTau");
                giuong.GiaVe = rs.getDouble("GiaVe");
                ds.add(giuong);
            }
            con.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {

        }
        return ds;
    }
    
    public static void Them(GiuongNam n) throws SQLException{
         Connection conn = ConnectionProvider.Connect();
        try{
          
            String sql = "INSERT INTO giuongnam(SoHieuGiuong, TrangThai, Tang, MaPhong, MaKhachHang, MaToa, MaTau, GiaVe) VALUES(?,?,?,?,?,?,?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1, n.SoHieuGiuong);
            pst.setString(2, n.TrangThai);
            pst.setInt(3, n.Tang);
            pst.setString(4, n.MaPhong);
            pst.setString(5, n.MaKhachHang);
            pst.setString(6, n.MaToa);
            pst.setString(7, n.MaTau);
            pst.setDouble(8, n.GiaVe);
            pst.executeUpdate();
            pst.close();
        }
        catch(Exception ex){
            ex.printStackTrace();
            ex.getMessage();
        }
        finally
        {
                conn.close();
        }
    }
    //Tim kiếm
    public static boolean TimKiemTatCa(int sohieu, int tang, String maphong, String matoa) {
        ArrayList<GiuongNam> ds = new ArrayList<GiuongNam>();
        Connection conn = ConnectionProvider.Connect();
        try {
            String sql = "Select  * from giuongnam where SoHieuGiuong = '" + sohieu + "' and Tang = '"+ tang +"' and MaPhong = '" + maphong + "' and MaToa = '" + matoa + "'";
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                GiuongNam giuong = new GiuongNam();
                giuong.SoHieuGiuong = rs.getInt("SoHieuGiuong");
                giuong.TrangThai = rs.getString("TrangThai");
                giuong.Tang = rs.getInt("Tang");
                giuong.MaPhong = rs.getString("MaPhong");
                giuong.MaKhachHang = rs.getString("MaKhachHang");
                giuong.MaToa = rs.getString("MaToa");
                giuong.MaTau = rs.getString("MaTau");
                giuong.GiaVe = rs.getDouble("GiaVe");
                ds.add(giuong);
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } 
        finally {
            if (ds.size() >= 1) {
                return true;
            } else {
                return false;
            }
        }
    }
}
